package com.example.controller;


import cn.hutool.crypto.SecureUtil;
import com.example.common.lang.Result;
import com.example.entity.*;
import com.example.service.CompanyService;
import com.example.service.StudentService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyk
 * @since 2022-02-19
 */
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    StudentService studentService;

    @Autowired
    CompanyService companyService;

    /**
     * 添加用户信息
     * @param user
     * @return
     */
    @PostMapping("/saveUser")
    public Result save(@Validated @RequestBody User user){
        User u = new User();
        u.setAccount(user.getAccount());
        u.setPassword(SecureUtil.md5(user.getPassword()));
        u.setStatus(user.getStatus());

        User account = userService.findByAccount(u.getAccount());

        if (account != null){
            return Result.succ(200,"该账号已被注册，请重新输入!",null);
        } else {
            int i = userService.saveUser(u);
            User u1 = userService.findID(u.getAccount());
            if (u1.getStatus() == 2){
                companyService.saveCompany(u1.getId());
            } else {
                studentService.saveStudent(u1.getId());
            }
            return Result.succ(200,"注册成功!",null);
        }
    }

    /**
     * 删除普通用户
     * @param id
     * @return
     */
    @GetMapping("/deleteStudentUser/{id}")
    public Result delete(@PathVariable(name = "id") Long id){

        Student student = studentService.studentById(id);
        Long l = student.getUserId();

        int i = userService.deleteUser(l);
        if (i > 0){
            studentService.deleteStudent(id);
            return Result.succ(200,"删除成功！",null);
        } else {
            return Result.succ(200,"删除失败！",null);
        }
    }

    /**
     * 删除企业用户
     * @param id
     * @return
     */
    @GetMapping("/deleteCompanyUser/{id}")
    public Result deleteCompanyUser(@PathVariable(name = "id") Long id){

        Company company = companyService.companyById(id);
        Long l = company.getUserId();

        int i = userService.deleteUser(l);
        if (i > 0){
            companyService.deleteCompany(id);
            return Result.succ(200,"删除成功！",null);
        } else {
            return Result.succ(200,"删除失败！",null);
        }
    }

    /**
     * 查询普通用户详细信息
     * @param id
     * @return
     */
    @GetMapping("/stuInformation/{id}")
    public Result stuInformation(@PathVariable(name = "id") Long id){

        StuInformation stuInformation = studentService.stuInformation(id);

        return Result.succ(stuInformation);
    }

    /**
     * 查询企业用户详细信息
     * @param id
     * @return
     */
    @GetMapping("/comInformation/{id}")
    public Result comInformation(@PathVariable(name = "id") Long id){

        ComInformation comInformation = companyService.comInformation(id);

        return Result.succ(comInformation);
    }
}
